package org.caowei.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.caowei.samples.model.Subject;
import org.caowei.samples.sevice.ISubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class SubjectController {

	@Autowired
	private ISubjectService subjectService;
	
	@ModelAttribute("subject")
	public Subject getSubject() {
		return new Subject();
	}
	
	@RequestMapping("/subjects")
	public String users(Model model) {
		try {
			List<Subject> list = subjectService.findAllSubjects();
			model.addAttribute("subjectList", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "subject/subjectList";
	}
	
	//获取所有当前科目关联的学生的后台方法
	@ResponseBody
	@RequestMapping(value = "/getAllStudent")
	public Map<String,Object> getAllStudent(Integer subjectId){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("allUser", subjectService.getAllStudent());//获取所有学生，包括没关联的
		resultMap.put("selectedUser", subjectService.getAllUserBySubject(subjectId));//获取所有已关联的学生
		return resultMap;
	}
	
	//保存修改
		@ResponseBody
		@RequestMapping(value = "/saveSubjectStudents")
		public Map<String,Object> saveSubjectStudents(@RequestParam(name = "subjectStudents[]")String[] subjectStudents,Integer subjectId){
			Map<String,Object> resultMap = new HashMap<String,Object>();
			resultMap.put("success", false);
			List<String> list = new ArrayList<String>();
			for(String student : subjectStudents){
				list.add(student);
			}
			try{
				subjectService.saveSubjectStudent(list,subjectId);
				resultMap.put("success", true);
			}catch(Exception e){
				e.printStackTrace();
			}
			return resultMap;
		}
		
		//更新按钮
		@ResponseBody
		@RequestMapping(value = "/tochangeStatus")
		public Map<String,Object> tochangeStatus(Integer subjectId){
			Map<String,Object> resultMap = new HashMap<String,Object>();
			resultMap.put("success", false);
			try{
				subjectService.TchangeStatus(subjectId);
				resultMap.put("success", true);
			}catch(Exception e){
				e.printStackTrace();
			}
			return resultMap;
		}
		
		
	
	@RequestMapping(value = "/addsubject", method = RequestMethod.GET)
	public String toAdd(Model model) {
		model.addAttribute("subject", new Subject());
		return "subject/addsubject";
	}

	@RequestMapping(value = "/addsubject", method = RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes, @Validated Subject subject, BindingResult result) {
		// if(result.hasErrors()){
		// return "user/add";
		// }
		try {
			subjectService.createSubject(subject);
			redirectAttributes.addFlashAttribute("message", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "redirect:/subjects";
	}
	
	@RequestMapping(value = "/editsubject/{id}", method = RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes, Subject subject, @PathVariable int id) {
		try {
			subject.setSubId(id);
			subjectService.editSubject(subject);
			redirectAttributes.addFlashAttribute("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "redirect:/subjects";
	}

	@RequestMapping(value = "/editsubject/{id}", method = RequestMethod.GET)
	public String toEdit(Model model, @PathVariable int id) {
		try {
			Subject subject = subjectService.getSubjectById(id);
			model.addAttribute("subject", subject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "subject/editsubject";
	}
	
	@RequestMapping(value = "/deletesubject/{id}")
	public String delete(RedirectAttributes redirectAttributes, @PathVariable int id) {
		try {
			subjectService.deleteSubject(id);
			redirectAttributes.addFlashAttribute("message", "删除成功");
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("message", "删除失败");
			e.printStackTrace();
		}
		return "redirect:/subjects";
	}
}
